import { Component, OnInit } from '@angular/core';
import { PageEntity } from '../../../core/domain/page.entity';
import { StatusEnum } from '../../../core/enum/status.enum';
import { SystemApprovalService } from './system-approval.service';
import {PaginationUtils} from '../../../utils/paginationUtils';
import {FilterEnum} from '../../../core/enum/filter.enum';
import {StorageUtil} from '../../../utils/storage.util';

@Component({
  selector: 'app-system-approval',
  templateUrl: './system-approval.component.html',
  styleUrls: ['./system-approval.component.scss'],
  providers: [SystemApprovalService]
})
export class SystemApprovalComponent implements OnInit {
  widthConfigs = [
    '60px', '180px', '200px', '150px', '150px',
    '150px', '150px', '150px', '240px', '200px',
    '150px', '180px', '150px', '150px', '120px'
  ];
  pageInfo: PageEntity = new PageEntity();
  allList = [];
  tabType = 0;
  zydxList = [
    {
      text: '中干'
    },
    {
      text: '老师'
    },
    {
      text: '学生'
    }
  ];
  deptList = [];
  pageIndex = 1;
  currObj = null;
  currType = null; // 0,驳回，1，通过，2，详情
  searchObj: SearObj = new SearObj();
  stateObj = {
    0: '新增',
    5: '修改',
    10: '停用'
  };

  constructor(
    private service: SystemApprovalService
  ) { }

  ngOnInit() {
    this.tabType = 0;
    this.getDept();
    this.resetSearch();
  }

  /*面板切换*/
  tabsChange(e) {
    this.tabType = e;
    this.resetSearch();
  }

  // 部门数据查询
  getDept() {
    this.service.getDept().subscribe(res => {
      if (res.status === StatusEnum.SUCCESS) {
        this.deptList = res.datas;
      }
    });
  }

  // 分页查询
  getPage() {
    if (this.tabType == 0) {
      this.pageInfo.filters.publishState = PaginationUtils.filters(this.tabType, FilterEnum.NOTEQ);
    } else {
      this.pageInfo.filters.publishState = PaginationUtils.filters(this.tabType, FilterEnum.EQUALS);
    }
    this.pageInfo.filters.isCheck = PaginationUtils.filters(1, FilterEnum.EQUALS);
    this.pageInfo.multiSortMeta = [
      {
        field: 'gmtCreate',
        order: -1
      }
    ];
    this.service.page(this.pageInfo).subscribe(res => {
      if (res.status === StatusEnum.SUCCESS) {
        this.allList = res.datas.data;
        this.pageInfo.totalRecords = res.datas.totalRecords;
      }
    });
  }

  // 第几页
  searchPageIndex(event) {
    this.pageInfo.first = event;
    this.getPage();
  }

  // 每页几条
  searchPageSize(event) {
    this.pageInfo.rows = event;
    this.pageInfo.first = 1;
    this.getPage();
  }

  backthis(e) {
    if (e) {
      this.getPage();
    }
    this.pageIndex = 1;
  }

  clickSearch() {
    for (const o in this.searchObj) {
      if (this.searchObj[o]) {
        if (o == 'publishDeptId') {
          this.pageInfo.filters[o] = PaginationUtils.filters(this.searchObj[o], FilterEnum.EQUALS);
        } else {
          this.pageInfo.filters[o] = PaginationUtils.filters(this.searchObj[o], FilterEnum.CONTAINS);
        }
      } else {
        delete this.pageInfo.filters[o];
      }
    }
    this.pageInfo.first = 1;
    this.getPage();
  }

  resetSearch() {
    this.searchObj = new SearObj();
    this.pageInfo = new PageEntity();
    this.getPage();
  }

  audit(data, type) {
    this.currObj = data;
    this.currType = type;
    this.pageIndex = 2;
  }

}

class SearObj {
  number?: string;
  name?: string;
  actionTarget?: string;
  publisherName?: string;
  deptName?: string;
}
